﻿
// JScript File
// Lấy thời gian còn lại
function GetTimeLeft(sDateEnd,sDateNow,iStatus)
{    
    var dateEnd = new Date(sDateEnd);
    var dateNow = new Date(sDateNow);
    var TimeValue = Math.ceil((dateEnd.getTime()-dateNow.getTime())/(1000));

    var _Time = TimeValue;   
    
	var days = Math.floor(TimeValue / (60 * 60 * 24));
	TimeValue %= (60 * 60 * 24);
	var hours = Math.floor(TimeValue / (60 * 60));
	TimeValue %= (60 * 60);
	var minutes = Math.floor(TimeValue / 60);
	TimeValue %= 60;
	var seconds = TimeValue;
	
	var dps = ''; var hps = ''; var mps = ''; var sps = '';
	
	if(days == 1) 
		dps ='';
	if(hours == 1) 
		hps ='';
	if(minutes == 1) 
		mps ='';
	if(seconds == 1) 
		sps ='';
	
	document.getElementById("divTimeLeft").innerHTML= '';		
	
	if(_Time <= 0 || iStatus > 2)
	{
	    document.getElementById("divTimeLeft").innerHTML= 'Kết thúc';		
	}	
	else
	{
	    document.getElementById("divTimeLeft").innerHTML = 'Còn ';
	    if(days)
	    {
		    document.getElementById("divTimeLeft").innerHTML += days + ' ngày' + dps + ' ';
		    document.getElementById("divTimeLeft").innerHTML += hours + ' giờ' + hps + ' ';
		    document.getElementById("divTimeLeft").innerHTML += minutes + ' phút' + mps + ' ';
		    document.getElementById("divTimeLeft").innerHTML += seconds + ' giây' + sps;
	    }	
	    else
	    {
		    if(hours)
		    {
			    document.getElementById("divTimeLeft").innerHTML += hours + ' giờ' + hps + ' ';
			    document.getElementById("divTimeLeft").innerHTML += minutes + ' phút' + mps + ' ';
			    document.getElementById("divTimeLeft").innerHTML += seconds + ' giây' + sps;
		    }
		    else
		    {
			    if(minutes)
			    {
				    document.getElementById("divTimeLeft").innerHTML += minutes + ' phút' + mps + ' ';
				    document.getElementById("divTimeLeft").innerHTML += seconds + ' giây' + sps;
			    }
			    else
			    {
				    document.getElementById("divTimeLeft").innerHTML += seconds + ' giây' + sps;
			    }
		    }
	    }
	    
	    dateNow.setSeconds(dateNow.getSeconds()+1);
	    setTimeout("GetTimeLeft('" + sDateEnd + "','"+ dateNow + "')", 1000);
	}
}



// Ham cho loading
 function Waiting(sDateEnd,sDateNow,iStatus)
{
    var obj = document.getElementById('divTimeLeft');
    if (obj!=null)
    {
       GetTimeLeft(sDateEnd,sDateNow,iStatus);
    }
    else
    {
        setTimeout("Waiting('"+sDateEnd+"','"+sDateNow+"','"+iStatus+"')",1000);        
    }
}
// Read Money to string
function ReadMoney(Money, CurrencyLabel)
{
    var temp = "";           
    Money = replaceAll(Money, '.', '');    
    Money = replaceAll(Money, ',', '');
    
    // Cho đủ 12 số
    while (Money.length < 12)
    {
        Money = "0" + Money;
    }
    
    var g1 = Money.substr(0, 3);
    var g2 = Money.substr(3, 3);
    var g3 = Money.substr(6, 3);
    var g4 = Money.substr(9, 3);

    //Đọc nhóm 1 ---------------------
    if (g1 != "000")
    {
        temp = ReadGroup3(g1);
        temp += " Tỷ";
    }
    //Đọc nhóm 2-----------------------
    if (g2 != "000")
    {
        temp += ReadGroup3(g2);
        temp += " Triệu";
    }
    //---------------------------------
    if (g3 != "000")
    {
        temp += ReadGroup3(g3);
        temp += " Ngàn";
    }
    //-----------------------------------
    //Chỗ này ko biết có nên ko ?
    temp = temp + ReadGroup3(g4).replace("Không Trăm Lẻ", "Lẻ"); // Đọc 1000001 là Một Triệu Lẻ Một thay vì Một Triệu Không Trăm Lẻ 1;
    //---------------------------------
    // Tinh chỉnh            
    temp = replaceAll(temp, "Một Mươi", "Mười");    
    temp = trimAll(temp);
    var _temp;
    if (temp.indexOf("Không Trăm") == 0)
    {
          temp = temp.replace("Không Trăm","");
    }
    temp = trimAll(temp);
    if (temp.indexOf("Lẻ") == 0)
    {   
        temp = temp.replace("Lẻ","");            
    }
    temp = trimAll(temp);
    temp = replaceAll(temp, "Mươi Một", "Mươi Mốt");
    temp = trimAll(temp);
    
    if(temp != "")
        temp += " " + CurrencyLabel;
    
    return temp;  
}

function ReadGroup3(G33)
{
    var _tam = " Không, Một, Hai, Ba, Bốn, Năm, Sáu, Bảy, Tám, Chín";
    var ReadDigit = _tam.split(',');    
    if (G33 == "000") return "";
    
    var G3 = G33.split('');    
        
    //Đọc số hàng trăm
    var temp = ReadDigit[Number(G3[0])] + " Trăm"; 
    //Đọc số hàng chục
    if (G3[1] == "0")
        if (G3[2] == "0") return temp;
        else
        {
            temp += " Lẻ" + ReadDigit[Number(G3[2])];
            return temp;
        }
    else
        temp += ReadDigit[Number(G3[1])] + " Mươi";
    //Đọc hàng đơn vị

    if (G3[2] == "5") temp += " Lăm";
    else if (G3[2] != "0") temp += ReadDigit[Number(G3[2])];
    
    return temp;
}

//Boundaries price.
function GetPrice(flag)
{
    var money;
    if(flag == 1)//Gia khoi diem thap nhat
        money = 1000; // mot ngan dong
    else if(flag == 2)//Gia khoi diem cao nhat
        money = 1000000000; //mot ty dong
    else if(flag == 3)//Gia mong muon thap nhat
        money = 2000; // hai ngan dong
    else if(flag == 4)//Gia mong muon cao nhat
        money = 100000000000; // mot tram ty.
    else if(flag == 5)//Gia mua ngay thap nhat
        money = 1000; // mot ngan
    else if(flag == 6)//Gia mua ngay cao nhat
        money = 100000000000; // mot tram ty.
    else
        money = 0;

    return money;
}





//Hàm lấy Query string 

function queryString(key)
{
    var page = new PageQuery(window.location.search); 
    return unescape(page.getValue(key)); 
}
function PageQuery(q) 
{
    if(q.length > 1) this.q = q.substring(1, q.length);
    else this.q = null;
    this.keyValuePairs = new Array();
    if(q) 
    {
        for(var i=0; i < this.q.split("&").length; i++) 
        {
            this.keyValuePairs[i] = this.q.split("&")[i];
        }
    }
    this.getKeyValuePairs = function() 
    { 
        return this.keyValuePairs; 
    }
    this.getValue = function(s) 
    {
        for(var j=0; j < this.keyValuePairs.length; j++) 
        {
            if(this.keyValuePairs[j].split("=")[0] == s)
                return this.keyValuePairs[j].split("=")[1];
        }
        return false;
    }
    this.getParameters = function() 
    {
        var a = new Array(this.getLength());
        for(var j=0; j < this.keyValuePairs.length; j++) 
        {
            a[j] = this.keyValuePairs[j].split("=")[0];
        }
        return a;
    }
    this.getLength = function() { return this.keyValuePairs.length; } 
}



// Hàm lấy giá trị sau dấu #
function getValueAfter()
{
    var id = self.document.location.hash.substring(1);
    return id;
}




function openPopup(theURL, theName, Width, Height)
{
	var strValue;
	var intLeft = parseInt((screen.width - Width) / 2);
	var intTop = parseInt((screen.height - Height) / 2);
	strValue = window.open(theURL, theName, 'help:no,width=' + Width + ',height=' + Height + ',left=' + intLeft + ',top=' + intTop + 'maximum=yes;status:No');
	return strValue;
}

function openPopup1(theURL, theName, Width, Height)
{
    var strValue;
	var intLeft = parseInt((screen.width - Width) / 2);
	var intTop = parseInt((screen.height - Height) / 2);
	strValue = window.open(theURL, theName, 'help:no,width=' + Width + ',height=' + Height + ',left=' + intLeft + ',top=' + intTop + ',scrollbars=yes');
	return strValue;
}


function openDialog(theURL, theName, Width, Height)
{
	var strValue;
	var strAppName = navigator.appName;
	var intLeft = parseInt((screen.width - Width) / 2);
	var intTop = parseInt((screen.height - Height) / 2);
	if(strAppName == 'Microsoft Internet Explorer')
		strValue = window.showModalDialog(theURL, theName, 'help:no;resizable: No; status: No; dialogHeight:' + Height + 'px;dialogWidth:' + Width + 'px;dialogLeft:' + intLeft + ';dialogTop:' + intTop + ';scrollbars=no;')
	else
		window.open(theURL, theName, 'help:no,width=' + Width + ',height=' + Height + ',left=' + intLeft + ',top=' + intTop);
	return strValue;
}

function CloseWindow(theForm)
{
	if(navigator.appName == 'Microsoft Internet Explorer')
	{
		if(CloseWindow.arguments.length == 2)
			window.returnValue = CloseWindow.arguments[1];
		else
			window.returnValue = 1;
		window.close();
	}
	else
	{
		eval(theForm).target = opener.name;
		eval(theForm).submit();
		window.close();
	}
}

function replaceAll(entry, before, after)
{
	out = before; // replace this
	add = after; // with this
	temp = "" + entry; // temporary holder

	while (temp.indexOf(out)>-1)
	{
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function countCharacter(str, chr){
	var i, count = 0;
	for(i = 0; i < str.length; i++){
		if(str.charAt(i) == chr)
			count++;
	}
	if(count == 1 || count == 0)
		return true;
	else
		return false;
}

function isCurrency(str){
	if(str.length == 0)
		return false;
	if(!isNumberCurrency(str))
		return false;
	if(!countCharacter(str, '.'))
		return false;
		
	return true;
}

function isVNCurrency(str)
{
    str = replaceAll(str, '.', '');
    str = replaceAll(str, ',', '');
    
	if(str.length == 0)
		return false;
	if(!isNumberCurrency(str))
		return false;
		
	return true;
}

function VNCurrency(str)
{
    str = replaceAll(str, '.', '');
    str = replaceAll(str, ',', '');
    return Number(str);
}

function isNumberCurrency(s){
	var i, isnum = true, len = s.length
	for(i = 0; i < len; i++){
		if(!isDigitCurrency(s.charAt(i)))
			isnum = false;
	}
	return isnum;
}

//Kiem tra trong luong

function isWeight(s){
	var i, isnum = true, len = s.length
	for(i = 0; i < len; i++){
		if(!isDigitCurrency(s.charAt(i)))
			isnum = false;
	}
	return isnum;
}

function isDigitCurrency(c){
	if((c=='0')||(c=='1')||(c=='2')||(c=='3')||(c=='4')||(c=='5')||(c=='6')||(c=='7')||(c=='8')||(c=='9')||(c=='.')||(c==','))
		return true;
	else
		return false;
}

function isNumber(s){
	var i, isnum = true, len = s.length
	for(i = 0; i < len; i++){
		if(!isDigit(s.charAt(i)))
			isnum = false;
	}
	return isnum;
}

function isNumberLenZero(s){
	var i, isnum = true, len = s.length
	for(i = 0; i < len; i++){
		if(!isDigit(s.charAt(i)))
			isnum = false;
	}
	return isnum;
}
function isDigit(c){
	if((c=='0')||(c=='1')||(c=='2')||(c=='3')||(c=='4')||(c=='5')||(c=='6')||(c=='7')||(c=='8')||(c=='9'))
		return true;
	else
		return false;
}

function isWeek(s){
	var i, isnum = true, len = s.length
	for(i = 0; i < len; i++){
		if(!isDigitWeek(s.charAt(i)))
			isnum = false;
	}
	return isnum;
}

function isDigitTime(c){
	if((c=='0')||(c=='1')||(c=='2')||(c=='3')||(c=='4')||(c=='5')||(c=='6')||(c=='7')||(c=='8')||(c=='9')||(c==':'))
		return true;
	else
		return false;
}

function countTimeChar(str, chr){
	var i, count = 0;
	for(i = 0; i < str.length; i++){
		if(str.charAt(i) == chr)
			count++;
	}
	if(count == 1)
		return true;
	else
		return false;
}

function isNumberTime(s){
	var i, isnum = true, len = s.length
	for(i = 0; i < len; i++){
		if(!isDigitTime(s.charAt(i)))
			isnum = false;
	}
	return isnum;
}

function isTime(s){
	var len = s.length
	
	if(len != 5 && len != 4)
		return false;
	if(!isNumberTime(s))
		return false;
	if(s.indexOf(':')==-1) 
		return false;
	if(s.indexOf(':')!=1 && s.indexOf(':')!=2)
		return false;
	if(!countTimeChar(s,':'))
		return false;
		
	return true;
}

function isDigitWeek(c){
	if((c=='1')||(c=='2')||(c=='3')||(c=='4')||(c=='5')||(c=='6')||(c=='7'))
		return true;
	else
		return false;
}

function trimLeft(s){
	var i;
	i = 0;
	var n;
	n = s.length;
	while((i<n)&&(s.charAt(i)==' ')) i++;
		s = s.substring(i);
	return(s);
} 

function trimRight(s){
	var n;
	n = s.length;
	var i;
	i = s.length-1;
	while((i>=0)&&(s.charAt(i)==' ')) i--;
		s = s.substring(0,i+1);
	return(s);
}

function trimAll(s){
	s = trimLeft(s);
	s = trimRight(s);
	return(s);
}  


function isPhoneNumber(s){
	var i;
	if(s.length == 0) return false;
	for(i = 0; i <= s.length-1; i++){
		if(!isDigit(s.charAt(i)) && s.charAt(i) != '(' && s.charAt(i) != ')' && s.charAt(i) != '-' && s.charAt(i) != ' ')
			return false;
	}
	return true;
}

function isEmail(emailStr){
	var emailPat = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	var matchArray = emailStr.match(emailPat);
	
	if (matchArray == null)
	{
		return (false);
	}

	return (true);
}

function isDate(dateStr) {
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2,4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        //alert("You must input date format mm/dd/yyyy"); //or dd-mm-yy
        return false;
    }

    month = matchArray[3]; // parse date into variables
    day = matchArray[1];
    year = matchArray[5];

    if (day < 1 || day > 31) {
        //alert("Day must from 1 to 31.");
        return false;
    }

    if (month < 1 || month > 12) { // check month range
        //alert("Month must from 1 to 12.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        //alert("This month have no 31 day!")
        return false;
    }

	if(year.length == 2)
		if(year < 20)
			year = parseInt('20' + year);
		else
			year = parseInt('19' + year);
	
    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            //alert("Month 2nd " + year + " have no " + day + " day!");
            return false;
        }
    }
 
    if(year < 1900)
		return false;
    return true; // date is valid
}


function isDate2(dateStr) {
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2,4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        //alert("You must input date format mm/dd/yyyy"); //or dd-mm-yy
        return false;
    }

    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[5];

    if (day < 1 || day > 31) {
        //alert("Day must from 1 to 31.");
        return false;
    }

    if (month < 1 || month > 12) { // check month range
        //alert("Month must from 1 to 12.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        //alert("This month have no 31 day!")
        return false;
    }

	if(year.length == 2)
		if(year < 20)
			year = parseInt('20' + year);
		else
			year = parseInt('19' + year);
	
    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            //alert("Month 2nd " + year + " have no " + day + " day!");
            return false;
        }
    }
 
    if(year < 1900)
		return false;
    return true; // date is valid
}